package com.ruoyi.project.lease.customer.service.impl;

import java.util.ArrayList;
import java.util.List;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.project.lease.order.domain.Order;
import com.ruoyi.project.lease.order.mapper.OrderMapper;
import com.ruoyi.project.system.user.domain.User;
import com.ruoyi.project.system.user.mapper.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.project.lease.customer.mapper.CustomerMapper;
import com.ruoyi.project.lease.customer.domain.Customer;
import com.ruoyi.project.lease.customer.service.ICustomerService;
import com.ruoyi.common.utils.text.Convert;

/**
 * 客户管理Service业务层处理
 * 
 * @author admin
 * @date 2021-12-31
 */
@Service
public class CustomerServiceImpl implements ICustomerService 
{
    @Autowired
    private CustomerMapper customerMapper;

    @Autowired
    private OrderMapper orderMapper;

    @Autowired
    private UserMapper userMapper;


    /**
     * 查询客户
     *
     * @param custNo 客户管理主键
     * @return 客户管理
     */
    @Override
    public Customer selectCustomerByCustNo(Long custNo)
    {
        return customerMapper.selectCustomerByCustNo(custNo);
    }

    /**
     * 查询客户管理通过手机号
     *
     * @param custPhone 客户管理主键
     * @return 客户管理
     */
    @Override
    public Customer selectCustomerByCustPhone(String custPhone){
        return customerMapper.selectCustomerByCustPhone(custPhone);
    }

    /**
     * 查询客户, 过滤del_flag
     * 
     * @param custNo 客户管理主键
     * @param delFlag 删除状态
     * @return 客户管理
     */
    @Override
    public Customer selectCustomerByCustNoDelFlag(Long custNo, int delFlag)
    {
        return customerMapper.selectCustomerByCustNoDelFlag(custNo, delFlag);
    }

    /**
     * 查询客户管理列表
     * 
     * @param customer 客户管理
     * @return 客户管理
     */
    @Override
    public List<Customer> selectCustomerList(Customer customer)
    {
        return customerMapper.selectCustomerList(customer);
    }

    /**
     * 查询所有客户,包括软删除的
     *
     * @return 客户管理集合
     */
    @Override
    public List<Customer> selectAllCustomer() {
        return customerMapper.selectAllCustomer();
    }


    /**
     * 查询所有状态正常且没被删除的客户
     *
     * @return 客户管理集合
     */
    @Override
    public List<Customer> selectNormalCustomer() {
        return customerMapper.selectNormalCustomer();
    }

    /**
     * 新增客户管理
     * 
     * @param customer 客户管理
     * @return 结果
     */
    @Override
    public int insertCustomer(Customer customer)
    {
        customer.setCreateTime(DateUtils.getNowDate());
        return customerMapper.insertCustomer(customer);
    }

    /**
     * 修改客户管理
     * 
     * @param customer 客户管理
     * @return 结果
     */
    @Override
    public int updateCustomer(Customer customer)
    {
        customer.setUpdateTime(DateUtils.getNowDate());
        return customerMapper.updateCustomer(customer);
    }

    /**
     * 批量删除客户管理
     * 
     * @param custNos 需要删除的客户管理主键
     * @return 结果
     */
    @Override
    public int deleteCustomerByCustNos(String custNos)
    {
        Long[] custNoList = Convert.toLongArray(custNos);
        int delCount = 0;
        // 订单为“待确认”、“租出”不允许删除
        for (Long custNo: custNoList) {
            List<Order> tmpOrderList = orderMapper.selectOrderByCustNoAndStatus(custNo, 0, 3);
            if (tmpOrderList.isEmpty()) {
                Customer customer = new Customer();
                customer.setCustNo(custNo);
                customer.setDelFlag(1);
                // 将客户状态设置为失效
                Customer customer1 = customerMapper.selectCustomerByCustNo(custNo);
                User user1 = userMapper.selectUserByLoginName(customer1.getCustPhone());
                user1.setStatus("1");
                userMapper.updateUser(user1);

                customerMapper.updateCustomer(customer);
                delCount += 1;
            }
        }
        return delCount;
    }

    /**
     * 删除客户管理信息
     * 
     * @param custNo 客户管理主键
     * @return 结果
     */
    @Override
    public int deleteCustomerByCustNo(Long custNo)
    {
        int delCount = 0;
        // 订单为“待确认”、“租出”不允许删除
        List<Order> tmpOrderList = orderMapper.selectOrderByCustNoAndStatus(custNo, 0, 3);
        if (tmpOrderList.isEmpty()) {
            Customer customer = new Customer();
            customer.setCustNo(custNo);
            customer.setDelFlag(1);
            customerMapper.updateCustomer(customer);
            delCount = 1;
        }
        return delCount;
    }

    /**
     * 查询本月新客户数
     *
     * @return 新客户数
     */
    @Override
    public Long selectThisMonthNewCust() {
        String monthFirstStr = DateUtils.getMonthFirstDay();
        String monthLastStr = DateUtils.getMonthLastDay();
        return customerMapper.selectThisMonthNewCust(monthFirstStr, monthLastStr);
    }

    @Override
    public int recoveryMode(String phoneNum) {
        return customerMapper.recoveryMode(phoneNum);
    }
}
